package com.jess.arms.base.my;

import android.net.ParseException;

import com.google.gson.JsonIOException;
import com.google.gson.JsonParseException;
import com.jess.arms.mvp.IView;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;

import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;


/**
 * 带加载框请求的回调处理
 *
 * @param <T>
 */
public abstract class BaseLoadingSubscriber<T> extends BaseSubscriber<T> {

    public BaseLoadingSubscriber(IView mRootView) {
        super(mRootView);
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseEntity<T> tBaseEntity) {
        if (tBaseEntity.isSuccess()) {
            onRequestSuccess(tBaseEntity.getData());
        } else {
            onRequestError(tBaseEntity.getMsg());
        }
    }

    @Override
    public void onError(Throwable t) {
        if (t instanceof UnknownHostException || t instanceof ConnectException || t instanceof UnknownServiceException) {
            onRequestError("网络出现了状况，请稍后重试");
        } else if (t instanceof SocketTimeoutException) {
            onRequestError("网络请求超时");
        } else if (t instanceof HttpException) {
            onRequestError("网络请求失败，请稍后重试");
        } else if (t instanceof JsonParseException || t instanceof ParseException || t instanceof JSONException || t instanceof JsonIOException) {
            onRequestError("数据解析错误 请稍后重试");
        } else {
            onRequestError("网络请求失败，请稍后重试");
        }
    }

    @Override
    public void onComplete() {

    }

}
